﻿Imports Cognex.VisionPro.ToolGroup
Imports Cognex.VisionPro.PMAlign
Imports Cognex.VisionPro.CalibFix
Imports Cognex.VisionPro
Imports System.Math

Public Class FrmSetting

    Private Sub FrmSetting_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            If gCurrProName <> "" Then
                FrmMain.initPro(gCurrProName)
                LblProductName.Text = gCurrProName
            End If
            Dim tg As New CogToolGroup
            For i = 0 To Cam_num - 1
                gTBCheck(i).Name = "检测工具" + (i + 1).ToString
                tg.Tools.Add(gTBCheck(i))
            Next

            CogTG.Subject = tg
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub 保存SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 保存SaveToolStripMenuItem.Click
        Try
            Dim i As Integer
            Dim path As String = gProDataPath & "\" & LblProductName.Text & "\"
            If Dir(path, FileAttribute.Directory) = "" Then '如果路径不存在,则创建路径
                MkDir(path)
            End If
            For i = 0 To Cam_num - 1
                If Dir(path, FileAttribute.Directory) = "" Then '如果路径不存在,则创建路径
                    MkDir(path)
                End If
                CogSerializer.SaveObjectToFile(gTBCheck(i), path & "TBCheck" & (i + 1).ToString & ".vpp")
            Next
            MsgBox("保存成功！")
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Private Sub 退出ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub 取消CancelToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 取消CancelToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub BtnEditName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEditName.Click
        LblProductName.Text = InputBox("请输出产品名称", LblProductName.Text)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' gComRecvData(0) = "SR,01,00" & TextBox1.Text
        FrmMain.SP1.Write(TextBox1.Text)
    End Sub

    Private Sub FrmSetting_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Dim path As String = gProDataPath & "\" & gCurrProName & "\"
        For i = 0 To Cam_num - 1
            CogSerializer.SaveObjectToFile(gTBCheck(i), path & "TBCheck后备份" & (i + 1).ToString & ".vpp")
        Next
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click, Button5.Click
        Dim CogCalibCheckerboardTool1 As CogCalibNPointToNPointTool = gTBCheck(0).Tools.Item("CogCalibNPointToNPointTool1")
        Dim CogPMAlignTool1 As CogPMAlignTool = gTBCheck(0).Tools.Item("CogPMAlignTool1")
        'If 1.5 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation And CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < 1.6 Then
        '    CogCalibCheckerboardTool1.Calibration.CalibratedOriginX += CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        '    CogCalibCheckerboardTool1.Calibration.CalibratedOriginY += CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
        'End If
        'If -1.6 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation And CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < -1.5 Then
        '    CogCalibCheckerboardTool1.Calibration.CalibratedOriginX -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        '    CogCalibCheckerboardTool1.Calibration.CalibratedOriginY -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
        'End If
        'If -0.1 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation And CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < 0.1 Then
        '    CogCalibCheckerboardTool1.Calibration.CalibratedOriginX += CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
        '    CogCalibCheckerboardTool1.Calibration.CalibratedOriginY -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        'End If
        'If 3.14 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation Then
        '    CogCalibCheckerboardTool1.Calibration.CalibratedOriginX -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
        '    CogCalibCheckerboardTool1.Calibration.CalibratedOriginY += CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        'End If
        'If CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < -3.14 Then
        '    CogCalibCheckerboardTool1.Calibration.CalibratedOriginX -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
        '    CogCalibCheckerboardTool1.Calibration.CalibratedOriginY -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        'End If


        CogCalibCheckerboardTool1.Calibration.Calibrate()
        CogCalibCheckerboardTool1.Run()
        CogPMAlignTool1.Run()
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, Button3.Click
        Dim CogCalibCheckerboardTool1 As CogCalibNPointToNPointTool = gTBCheck(1).Tools.Item("CogCalibNPointToNPointTool1")
        Dim CogPMAlignTool1 As CogPMAlignTool = gTBCheck(1).Tools.Item("CogPMAlignTool1")
        If 1.5 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation And CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < 1.6 Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX += CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY += CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
        End If
        If -1.6 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation And CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < -1.5 Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
        End If
        If -0.1 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation And CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < 0.1 Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX += CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        End If
        If 3.14 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY += CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        End If
        If CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < -3.14 Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY += CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        End If


        CogCalibCheckerboardTool1.Calibration.Calibrate()
        CogCalibCheckerboardTool1.Run()
        CogPMAlignTool1.Run()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim CogCalibCheckerboardTool1 As CogCalibNPointToNPointTool = gTBCheck(2).Tools.Item("CogCalibNPointToNPointTool1")
        Dim CogPMAlignTool1 As CogPMAlignTool = gTBCheck(2).Tools.Item("CogPMAlignTool1")

        If 1.5 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation And CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < 1.6 Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX += CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY += CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
        End If
        If -1.6 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation And CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < -1.5 Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
        End If
        If -0.1 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation And CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < 0.1 Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX += CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        End If
        If 3.14 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY += CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        End If
        If CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < -3.14 Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY += CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        End If


        CogCalibCheckerboardTool1.Calibration.Calibrate()
        CogCalibCheckerboardTool1.Run()
        CogPMAlignTool1.Run()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim CogCalibCheckerboardTool1 As CogCalibNPointToNPointTool = gTBCheck(3).Tools.Item("CogCalibNPointToNPointTool1")
        Dim CogPMAlignTool1 As CogPMAlignTool = gTBCheck(3).Tools.Item("CogPMAlignTool1")
        If 1.5 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation And CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < 1.6 Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX += CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY += CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
        End If
        If -1.6 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation And CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < -1.5 Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
        End If
        If -0.1 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation And CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < 0.1 Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX += CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        End If
        If 3.14 < CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY += CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        End If
        If CogCalibCheckerboardTool1.Calibration.CalibratedXAxisRotation < -3.14 Then
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginX -= CogPMAlignTool1.Results.Item(0).GetPose.TranslationX
            CogCalibCheckerboardTool1.Calibration.CalibratedOriginY += CogPMAlignTool1.Results.Item(0).GetPose.TranslationY
        End If


        CogCalibCheckerboardTool1.Calibration.Calibrate()
        CogCalibCheckerboardTool1.Run()
        CogPMAlignTool1.Run()
    End Sub
End Class